package com.ng.desktop.hr.view.cases;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.apache.commons.lang.StringUtils;

import com.ng.desktop.hr.model.Cases;
import com.ng.desktop.hr.model.CaseContactType;
import com.ng.desktop.hr.model.CasePriority;
import com.ng.desktop.hr.model.CaseState;
import com.ng.desktop.hr.model.Customer;
import com.ng.desktop.hr.persistence.CasePersistence;
import com.ng.desktop.hr.persistence.CustomerPersistence;
import com.ng.desktop.hr.services.HRListingService;
import com.ng.desktop.hr.services.NGFactoryBean;
import com.ng.desktop.hr.view.customer.CustomerEntitySelector;
import com.ng.framework.persistence.NGListingBean;
import com.ng.framework.util.NGUtil;
import com.ng.framework.view.desktop.NGCombo;
import com.ng.framework.view.desktop.NGDatePicker;
import com.ng.framework.view.desktop.NGEditDialog;
import com.ng.framework.view.desktop.NGEntitySelectorField;

public class CaseEditDialog extends NGEditDialog<Cases, CasePersistence> {

	private static final long serialVersionUID = -8491323457814105705L;

	private JTextField textFieldId;
	private JTextField textFieldTitle;
	private NGEntitySelectorField<Customer, CustomerPersistence> customerSelection;
	private JTextField textFieldCreationDate;
	private NGDatePicker textFieldEstimatedResolutionDate;
	private JTextField textFieldRealResolutionDate;
	private NGCombo comboCaseState;
	private NGCombo comboCasePriority;
	private NGCombo comboCaseContactType;
	private NGCombo comboOut;
	private JTextArea textAreaObservations;

	public CaseEditDialog(Frame owner, CasePersistence persistence) {
		this(owner, persistence, null);
	}

	public CaseEditDialog(Frame owner, CasePersistence persistence, Cases instance) {
		super(owner, persistence, instance);

		this.setTitle("Gestión de Casos");
		this.setSize(new Dimension(600, 600));
		this.setResizable(false);
	}

	@Override
	protected JPanel createDetailPanel() {
		this.textFieldId = new JTextField(5);
		textFieldId.setEditable(false);
		textFieldId.setFocusable(false);
		textFieldId.setHorizontalAlignment(JTextField.RIGHT);
		this.textFieldTitle = new JTextField();
		this.customerSelection = new CustomerEntitySelector();

		this.textFieldCreationDate = new JTextField(7);
		this.textFieldCreationDate.setHorizontalAlignment(JTextField.LEFT);
		this.textFieldCreationDate.setEditable(false);
		this.textFieldCreationDate.setFocusable(false);

		this.textFieldEstimatedResolutionDate = new NGDatePicker();
		this.textFieldEstimatedResolutionDate.setHorizontalAlignment(JTextField.LEFT);

		this.textFieldRealResolutionDate = new JTextField(7);
		this.textFieldRealResolutionDate.setEditable(false);
		this.textFieldRealResolutionDate.setFocusable(false);
		this.textFieldRealResolutionDate.setHorizontalAlignment(JTextField.LEFT);

		comboCaseState = new NGCombo();
		comboCaseState.addNGItems(getListingService().getList(HRListingService.CASE_STATES, null, null));
		comboCasePriority = new NGCombo();
		comboCasePriority.addNGItems(getListingService().getList(HRListingService.CASE_PRIORITIES, null, null));
		comboCaseContactType = new NGCombo();
		comboCaseContactType.addNGItems(getListingService().getList(HRListingService.CASE_CONTACT_TYPE, null, null));
		comboOut = new NGCombo();
		comboOut.addNGItems(Arrays.asList("Enviado", "Recibido"));

		textAreaObservations = new JTextArea();
		textAreaObservations.setLineWrap(true);
		textAreaObservations.setWrapStyleWord(true);

		JPanel detailPanel = new JPanel(new GridBagLayout());

		detailPanel.add(new JLabel("Id:"), new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(textFieldId, new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.NONE,
				new Insets(0, 0, 5, 0), 0, 0));

		detailPanel.add(new JLabel("Título:"), new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(textFieldTitle, new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.WEST,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 0), 0, 0));

		detailPanel.add(new JLabel("Cliente:"), new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(customerSelection, new GridBagConstraints(1, 2, 1, 1, 1, 0, GridBagConstraints.WEST,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 0), 0, 0));

		detailPanel.add(new JLabel("Fecha de Creación:"), new GridBagConstraints(0, 3, 1, 1, 0, 0, GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(textFieldCreationDate, new GridBagConstraints(1, 3, 1, 1, 1, 0, GridBagConstraints.WEST,
				GridBagConstraints.NONE, new Insets(0, 0, 5, 0), 0, 0));

		detailPanel.add(new JLabel("Resolución Estimada:"), new GridBagConstraints(0, 4, 1, 1, 0, 0, GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(textFieldEstimatedResolutionDate, new GridBagConstraints(1, 4, 1, 1, 1, 0, GridBagConstraints.WEST,
				GridBagConstraints.NONE, new Insets(0, 0, 5, 0), 0, 0));

		detailPanel.add(new JLabel("Resolución Real:"), new GridBagConstraints(0, 5, 1, 1, 0, 0, GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(textFieldRealResolutionDate, new GridBagConstraints(1, 5, 1, 1, 1, 0, GridBagConstraints.WEST,
				GridBagConstraints.NONE, new Insets(0, 0, 5, 0), 0, 0));

		detailPanel.add(new JLabel("Recibido/Enviado:"), new GridBagConstraints(0, 6, 1, 1, 0, 0, GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(comboOut, new GridBagConstraints(1, 6, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.RELATIVE,
				new Insets(0, 0, 5, 0), 0, 0));

		detailPanel.add(new JLabel("Estado:"), new GridBagConstraints(0, 7, 1, 1, 0, 0, GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(comboCaseState, new GridBagConstraints(1, 7, 1, 1, 1, 0, GridBagConstraints.WEST,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 0), 0, 0));

		detailPanel.add(new JLabel("Prioridad:"), new GridBagConstraints(0, 8, 1, 1, 0, 0, GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(comboCasePriority, new GridBagConstraints(1, 8, 1, 1, 0.5, 0, GridBagConstraints.WEST,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 100), 0, 0));

		detailPanel.add(new JLabel("Tipo de Contacto:"), new GridBagConstraints(0, 9, 1, 1, 0, 0, GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(comboCaseContactType, new GridBagConstraints(1, 9, 1, 1, 1, 0, GridBagConstraints.WEST,
				GridBagConstraints.NONE, new Insets(0, 0, 5, 0), 0, 0));

		JScrollPane scrollPane = new JScrollPane(textAreaObservations, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setBorder(BorderFactory.createTitledBorder("Observaciones:"));
		detailPanel.add(scrollPane, new GridBagConstraints(0, 11, 2, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 0, 0), 0, 0));

		return detailPanel;
	}

	@Override
	protected boolean validateInput() {

		boolean valid = true;

		if (StringUtils.isEmpty(textFieldTitle.getText())) {
			JOptionPane.showMessageDialog(this, "El campo 'Título' es obligatorio.", "Error", JOptionPane.ERROR_MESSAGE);
			textFieldTitle.requestFocus();

			valid = false;
		} else if (customerSelection.getEntity() == null) {
			JOptionPane.showMessageDialog(this, "El campo 'Cliente' es obligatorio.", "Error", JOptionPane.ERROR_MESSAGE);
			textFieldCreationDate.requestFocus();

			valid = false;
		}
		return valid;
	}

	private HRListingService getListingService() {
		return (HRListingService) NGFactoryBean.getInstance().getBean("hrListingService");
	}

	@Override
	protected Cases createNewInstance() {
		return new Cases();
	}

	@Override
	protected void populateInstance(Cases instance) {

		if (!isCreationMode()) {
			instance.setId(Long.valueOf(textFieldId.getText()));

		} else {
			instance.setCreationDate(new Date());
		}
		instance.setTitle(textFieldTitle.getText());
		instance.setCustomer(customerSelection.getEntity());
		try {
			instance.setEstimatedResolutionDate(NGUtil.ddMMyyyy_dateFormat.parse(textFieldEstimatedResolutionDate.getText()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		instance.setCaseState(getListingService().getHydratedBean(comboCaseState.getSelectedNGItem(NGListingBean.class),
				CaseState.class));
		instance.setCasePriority(getListingService().getHydratedBean(comboCasePriority.getSelectedNGItem(NGListingBean.class),
				CasePriority.class));
		instance.setCaseContactType(getListingService().getHydratedBean(
				comboCaseContactType.getSelectedNGItem(NGListingBean.class), CaseContactType.class));
		instance.setOut(comboOut.getSelectedItem().toString().equals("Enviado") ? true : false);
		instance.setObservations(textAreaObservations.getText());

		if (instance.getCaseState().getDescription().equals("Finalizado")
				|| instance.getCaseState().getDescription().equals("Cerrado")) {
			if (instance.getRealResolutionDate() == null) {
				instance.setRealResolutionDate(new Date());
			}

		}

	}

	@Override
	protected void populateDetailPanel(Cases instance) {

		if (!isCreationMode()) {
			textFieldId.setText(String.valueOf(instance.getId()));
		}
		textFieldCreationDate.setText(NGUtil.ddMMyyyy_dateFormat.format(instance.getCreationDate()));
		textFieldTitle.setText(instance.getTitle());
		customerSelection.setEntity(instance.getCustomer());
		if (instance.getEstimatedResolutionDate() != null) {
			textFieldEstimatedResolutionDate.setText(NGUtil.ddMMyyyy_dateFormat.format(instance.getEstimatedResolutionDate()));
		}
		comboCaseState.setSelectedNGItem(instance.getCaseState());
		comboCasePriority.setSelectedNGItem(instance.getCasePriority());
		comboCaseContactType.setSelectedNGItem(instance.getCaseContactType());
		comboOut.setSelectedNGItem(instance.getOut() ? "Enviado" : "Recibido");
		textAreaObservations.setText(instance.getObservations());
		if (instance.getRealResolutionDate() != null) {
			textFieldRealResolutionDate.setText(NGUtil.ddMMyyyy_dateFormat.format(instance.getRealResolutionDate()));
		}

	}

}
